﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode
{
    class P0036 : IProblem
    {
        public void Run()
        {
            var s = new Solution();

        }
        public class Solution
        {
            public bool IsValidSudoku(char[][] board)
            {
                HashSet<int> set = new HashSet<int>();
                for (int i = 0; i < board.GetLength(0); i++)
                {
                    for (int j = 0; j < board.GetLength(1); j++)
                    {
                        var cur = board[i][j];
                        if (cur == '.')
                            continue;
                        int v = cur - '0';
                        if (set.Contains(v))
                            return false;
                        set.Add(v);
                    }
                    set.Clear();
                }
                set.Clear();
                for (int i = 0; i < board.GetLength(0); i++)
                {
                    for (int j = 0; j < board.GetLength(1); j++)
                    {
                        var cur = board[j][i];
                        if (cur == '.')
                            continue;
                        int v = cur - '0';
                        if (set.Contains(v))
                            return false;
                        set.Add(v);
                    }
                    set.Clear();
                }
                set.Clear();
                for (int t = 0; t < 9; t++)
                {
                    int x = t % 3;
                    int y = t / 3;

                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            var cur = board[i + x * 3][j + y * 3];
                            if (cur == '.')
                                continue;
                            int v = cur - '0';
                            if (set.Contains(v))
                                return false;
                            set.Add(v);
                        }
                    }
                    set.Clear();
                }

                return false;
            }
        }
    }
}
